﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ADiKt {
    class AI : Worm {
        int[] dirToTest = { -1, 0, 1 };
        int[] distToCrash = { 0, 0, 0 };
        Dictionary<int, int> difficultySearch = new Dictionary<int, int>() { { 0, 8 }, { 1, 16 }, { 2, 30 }, { 3, 45 }, { 4, 50 } };
        public int difficulty = 2;

        public AI(Color colour) {
            this.colour = colour;
            this.colourName = Globals.colListFree[colour];
            Globals.colListUsed.Add(colour, colourName);
            Globals.colListFree.Remove(colour);
            Globals.worms.Add(this);
            reset();
        }

        private int getMaxLife2(int[] dist) {
            int max;
            if (dist[0] == dist[1] && dist[2] == dist[1]) {
                return (int)Math.Round(Globals.rand.NextDouble() * 2f - 1f, 0);
            } else if (dist[0] == dist[2] && dist[0] > dist[1]) {
                return (-1) ^ ((int)Math.Round(Globals.rand.NextDouble() + 1f, 0));
            } else if (dist[0] > dist[1]) {
                return max = (dist[0] > dist[2]) ? -1 : 1;
            } else {
                return max = (dist[2] > dist[1]) ? 1 : 0;
            }
        }

        private int getMaxLife(int[] dist) {
            if (dist[1] < dist[0] && dist[1] < dist[2]) {
                return 1;
            } else {
                double temp = (dist[2] - dist[0]) / (double)(dist[0] + dist[1] + dist[2]);
                //System.Console.WriteLine(temp); 
                if (temp < -0.05) {
                    return -1;
                } else if (temp < 0.05) {
                    return 0;
                } else {
                    return 1;
                }
            }
        }

        /// <summary> checks whether a line from start to end intersects any other line on the screen </summary>
        private bool lineCrash(Vector2 start, Vector2 end) {
            List<RoundLine> rls = Globals.points[(int)((double)Globals.gridSize * start.X / (double)Globals.width), (int)((double)Globals.gridSize * start.Y / (double)Globals.height)];

            foreach (RoundLine rl in rls) {
                if (rl.doLinesIntersect(start, end) && !this.bodyLines.Contains(rl))
                    return true;
            }
            return false;
        }

        public override void turn(KeyboardState ks) {
            int maxDist = difficultySearch[difficulty];
            Vector2 testpos;
            Vector2 oldTestPos;
            double testAngle;
            distToCrash = new int[] { 0, 0, 0 };
            int index = 0;

            foreach (int dir in dirToTest) {
                testAngle = angle;
                testpos = pos;
                oldTestPos = pos;
                for (int j = 1; j <= maxDist; j++) {
                    testAngle += (dir * Globals.turn_step);
                    testpos += new Vector2((float)(length * Math.Cos(testAngle)), (float)(length * Math.Sin(testAngle)));

                    if (testpos.X < 0 || testpos.X > Globals.width || testpos.Y < 0 || testpos.Y > Globals.height || collision(oldTestPos, testpos) || j == maxDist) {
                        distToCrash[index] = j;
                        break;
                    }
                    oldTestPos = testpos;
                }
                index++;
            }
            //System.Console.WriteLine(this.colourName + ", " + distToCrash[0] + ", " + distToCrash[1] + ", " + distToCrash[2] + ", " + getMaxLife(distToCrash));
            angle += Globals.turn_step * getMaxLife(distToCrash);
        }
    }
}